//
//  BIZHotTopicsParentListVC.m
//  biz
//
//  Created by zhangfuwei on 2017/7/7.
//  Copyright © 2017年 jieshuibao. All rights reserved.
//

#import "BIZHotTopicsParentListVC.h"
#import "BIZHotTopicChildrenListVC.h"
#import "BIZHotTopicDetailVC.h"

//cell
#import "BIZTopicImageCell.h"
#import "BIZTopicTextCell.h"
//model
#import "BIZHotTopicParentListModel.h"

@interface BIZHotTopicsParentListVC ()<UITableViewDataSource,UITableViewDelegate>

/** talbeView */
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation BIZHotTopicsParentListVC {
    BIZHotTopicParentListModel  *_listM;
    NSString    *_uuid;
    int         _lastID;
}

-(instancetype)initWithUUID:(NSString *)uuid {
    self = [super init];
    if (self) {
        _uuid = uuid;
    }
    return self;
}

#pragma mark -- 视图生命周期

- (void)zj_viewDidLoadForIndex:(NSInteger)index {
    [self initSubViews];
//    _lastID = 1;
//    [self requestHotTopicParentListWithPageIndex:_lastID pageEnd:_lastID+9];
    [_tableView.mj_header beginRefreshing];
    
}

//-(void)zj_viewWillAppearForIndex:(NSInteger)index {
//    
//}
//
//- (void)zj_viewWillDisappearForIndex:(NSInteger)index {
//    
//}

- (void)initSubViews {
    
    [self.view addSubview:self.tableView];
}

#pragma mark -- requset
- (void)requestHotTopicParentListWithPageIndex:(int)idx pageEnd:(int)end {
    
    MJWeakSelf;
    [RequestTool POST:kHotTopicIssuesParentURL
           parameters:@{@"uuid":_uuid,@"pageIndex":@(idx),
                        @"pageEnd":@(end)}
             progress:nil
              success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
                  [weakSelf stopRefresh];
                  
                  DeLog(@"热点问题(父级)--%@",responseObject);
                  BIZHotTopicParentListModel  *model = [BIZHotTopicParentListModel yy_modelWithJSON:responseObject];
                  if (model.code.intValue == 1) {
                      if (idx == 1) {
                          _listM = model;
                      }else {
                          _listM.data.hotIssues = [_listM.data.hotIssues arrayByAddingObjectsFromArray:model.data.hotIssues];
                      }
                      _lastID += model.rows;
                      [_tableView reloadData];
                      
                  }else if (model.code.intValue == 2){
                      [MBProgressHUD showTipMessageInView:@"请登录"];
                      [BIZUserModel shareUser].login = NO;
                      [weakSelf presentViewController:[BIZLoginViewController viewControllerFromStoryboard] animated:YES completion:nil];
                      
                  }else {
                      [MBProgressHUD showErrorMessage:model.des];
                      
                  }
              }
              failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
                  [MBProgressHUD showErrorMessage:@"网络不给力,请检测网络" timer:2.5];
                  [weakSelf stopRefresh];
                  DeLog(@"error ---%@",error);
                  
              }
     ];
}

#pragma mark -- tableView数据源&& 代理
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _listM.data.hotChannel.count + _listM.data.hotIssues.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger channelCount  = _listM.data.hotChannel.count;

    if (indexPath.row < channelCount) {
         BIZTopicImageCell *cell = [BIZTopicImageCell cellWithTableView:tableView indexPath:indexPath hidden:NO];
        cell.channelModel = _listM.data.hotChannel[indexPath.row];
        return cell;
    }
    
    BIZTopicTextCell *cell = [BIZTopicTextCell cellWithTableView:tableView indexPath:indexPath];
    cell.issueModel = _listM.data.hotIssues[indexPath.row-channelCount];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row < _listM.data.hotChannel.count) {
        return 197;
    }
    return UITableViewAutomaticDimension;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger channelCount  = _listM.data.hotChannel.count;
    if (indexPath.row < channelCount) {
        BIZHotTopicChildrenListVC *listVC = [[BIZHotTopicChildrenListVC alloc] initWithChannelModel:_listM.data.hotChannel[indexPath.row]];
        [self.navigationController pushViewController:listVC animated:YES];
        return;
    }
    BIZHotTopicDetailVC *detailVC = [[BIZHotTopicDetailVC alloc] initWithUUID:_listM.data.hotIssues[indexPath.row-channelCount].uuid];
    [self.navigationController pushViewController:detailVC animated:YES];
    
}


#pragma mark -- lazyload

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height-64) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.estimatedRowHeight = 50;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        [_tableView registerClass:[BIZTopicImageCell class] forCellReuseIdentifier:kTopicImageCellID];
        [_tableView registerClass:[BIZTopicTextCell class] forCellReuseIdentifier:kTopicTextCellID];
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            _lastID = 1;
            [self requestHotTopicParentListWithPageIndex:_lastID pageEnd:_lastID+9];
        }];
        
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [self requestHotTopicParentListWithPageIndex:_lastID pageEnd:_lastID+9];
        }];

    }
    return _tableView;
}

- (void)stopRefresh
{
    !self.tableView.mj_header.isRefreshing ? : self.tableView.mj_header.endRefreshing;
    !self.tableView.mj_footer.isRefreshing ? : self.tableView.mj_footer.endRefreshing;
}

@end
